$(function(){
	var prev = 0;
	var animalPhoto ="";
	var foodPhoto = "";
	var startPhoto = "";
	var imgArr = [];
	// 倒退按钮事件
	$(".goback").click(function(){
		history.go(-1);
	});
	// 加载完页面后给 选择按钮一个动画效果
	$(".sBtn").addClass("animated bounceIn");
	$(".goback").addClass("animated bounceIn");
	
	// 选择默认上传事件函数 封装 
	function isLoad(){
			if(!$(".icon img").attr("src")){
				$.ajax({
					type:"post",
					url:"/settingUPhoto",
					async:true
				}).done(function(status,resData,jqXHR){
					if(resData){
						 animalPhoto = resData.animalPhoto;
						 foodPhoto = resData.foodPhoto;
						 startPhoto = resData.startPhoto;
						imgArr = [animalPhoto,foodPhoto,startPhoto];
						$(".icon").each(function(index){
							$(this).find("img").attr("src",animalPhoto[index]);
						});
						$(".icon-group ul").css({backgroundImage:"url()",borderColor:"lightblue"});
					}else{
						$(".icon-group ul p").text("加载头像失败").show();
						$(".icon-group ul").css({backgroundImage:"url()",borderColor:"lightcoral"});
						
					}
				}).fail(function(jqXHR,status,error){
					$(".icon-group ul p").text("网络连接失败").show();
					$(".iSure").off("click").next().off("click");
					$(".icon-group ul").css({backgroundImage:"url()",borderColor:"lightcoral"});
				});
			}
	}
	// 出现选择自定义头像动画事件
	function customPhoto(obj){
			setTimeout(function(){
				autoPosition();
				$(".fixTop").slideDown();
				$(".showImg").fadeIn();
				$(".imgContainer").show();
				$(".sIcon").data("next",false).fadeIn().text(obj.text).css({backgroundColor:obj.bgcolor,borderColor:obj.color});
			},1000);
		}
	
	
	// 点击选择默认头像事件
	$(".sDPhoto").on("click",function(){
		var obj = {
			text:"选择上传头像",
			bgcolor:"lightblue",
			color:"white"
		};
		$(this).parents(".sBtn").addClass("bounceOut");
		customPhoto(obj)
		isLoad();
	});
	

	
	// 点击确定按钮上传头像
	$(".iSure").on("click",function(){
		var data = $(".showImg img").attr("src");
		if(data){
				if($(".reSure").css("display") == "block"){
					$(".imgContainer ul").removeClass("animated flipInY").addClass("animated wobble");
					setTimeout(function(){
						$(".imgContainer ul").removeClass("animated wobble")
					},1000);
					return;
				}
			$(".imgContainer ul").css("background","rgba(0,0,0,0.8)").addClass("animated flipInY").find(".reSure").show().end().find(".icon").hide();
			setTimeout(function(){
				$(".imgContainer ul").removeClass("animated flipInY");
			},1000);
		}else{
			$(".imgContainer ul").css("background","rgba(0,0,0,0.8)").addClass("animated flipInY").find("p").text("未选择任何头像").show().end().find(".icon").hide();
			setTimeout(function(){
				$(".imgContainer ul").removeClass("animated flipInY");
			},500);
			setTimeout(function(){
				$(".imgContainer ul").css("background","").addClass("animated flipInY").find("p").hide().end().find(".icon").show();
			},1500)
			setTimeout(function(){
				$(".imgContainer ul").removeClass("animated flipInY");
			},2000)
			
		}
	});
	
	// 点击确定上传上传头像
	$(".reSure span").eq(0).on("click",function(){
		var data = $(".showImg img").attr("src");
		$.ajax({
			type:"post",
			url:"/uploadPhoto",
			async:true,
			data
		}).done(function(status,resData,jqXHR){
			
		}).fail(function(jqXHR,status,error){
			$(".imgContainer ul").find("p").text("上传失败").show().end().find(".reSure").hide();
			setTimeout(function(){
				$(".imgContainer ul").css("background","").addClass("animated flipInY").find("p").hide().end().find(".icon").show();
			},1500);
			setTimeout(function(){
				$(".imgContainer ul").removeClass("animated flipInY");
			},2000);
		});
	});
	
	
	// 点击取消上传头像事件
	$(".reSure span").eq(1).on("click",function(){
		$(this).parent().hide();
		$(".imgContainer ul").css("background","").addClass("animated flipInY").find(".icon").show();
	});
	
	
	// 顶部选择头像事件
	$(".selectBtn").click(function(event){
		var index = $(this).index();
		$(this).addClass("activeWC").siblings().removeClass("activeWC");
		if(prev != index){
			prev = index;
			$(".imgContainer").removeClass("animated fadeInDown").removeClass("animated flipInY").addClass("animated hinge");
			setTimeout(function(){
				$(".imgContainer").removeClass("animated hinge").removeClass("animated flipInY").addClass("animated fadeInDown");
			},2000);
		}
			$(".icon").each(function(sindex){
					$(this).find("img").attr("src",imgArr[index][sindex]);
				});
	});
	// 选择默认头像的头像显示区域事件
	$(".showImg").on("click","img",function(){
		$(this).addClass("animated flip");
		var $this = $(this);
		setTimeout(function(){
			$this.removeClass("animated flip");
		},1000);
	});

	

	// 点击头像在显示区域显示
	$(".imgContainer").on("click",".icon",function(event){
		var imgUrl = $(this).find("img").attr("src");
		$(".showImg").find("img").attr("src",imgUrl).next().hide();
	});
	
	// 样式居中
	function autoPosition(){
		var vx = ($(window).width()-240)/2;
//		alert(vx);
		$(".fixTop .classify").css("left",vx);
		var ux = ($(".uSr").width()-150) / 2;
		$(".uSr a").css("margin-left",ux);
		var sx = ($(window).width() - 110) / 2;
		var vh =$("body")[0].scrollHeight;
		$(".sIcon").css({left:sx,top:vh});
		var rx = $(".imgContainer").width()/ 2 -105;
		$(".reSure").css("margin-left",rx);
	}
	
	$(window).resize(autoPosition);
	
	// 点击取消 显示头像区域为空
	$(".iCancel").on("click",function(){
		if($(".reSure").css("display") == "block"){
				$(".imgContainer ul").removeClass("animated flipInY").addClass("animated wobble");
				setTimeout(function(){
					$(".imgContainer ul").removeClass("animated wobble")
				},1000);
		}else{
			$(".showImg").find("img").attr("src","").next().show();
		}
	});
	
	// 浏览选择图片按钮调用文件框事件
	$(".uSr a").on("click",function(){
		$(this).css("background-color","lightcoral").parent().next().click();
	});
	
	
	// 选择默认头像事件
	function changeUpload(num){
		if(num){
			$(".fixTop").slideUp();
			$(".showImg").fadeOut();
			$(".imgContainer").addClass("animated fadeOutUp");
			setTimeout(function(){
				$(".imgContainer").hide().removeClass("animated fadeOutUp");
			},1000);
			uoloadP();
			$(".sIcon").text("选择默认头像").css("background-color","#D9534F");
		}else{
				$(".sBtn").addClass("bounceOut");
				$("section").show().removeClass("animated rollOut").addClass("animated rollIn");
				
		}
	}
	function uoloadP(){
		changeUpload(0);
		autoPosition();
		setTimeout(function(){
			$(".sIcon").data("next",true).slideDown().css("top",0)
		},1000);
	}
	
	// 选择上传头像区域事件
	$(".uploadPhoto").click(function(){
		uoloadP();
	});
	
	$(".sIcon").on("click",function(){
		if($(this).data("next")){
			$("section").removeClass("animated rollIn").addClass("animated rollOut");
			$(this).data("next",false);
			isLoad();
			var obj = {
				text:"选择上传头像",
				bgcolor:"lightblue",
				color:"white"
			}
			customPhoto(obj);
		}else{
			changeUpload(1);
			$(this).data("next",true);
		}
		
	});
	
	// fail  
	$("#userPhoto").on("change",function(){
		var file = $(this)[0].files[0];
		$(".uSl img").attr("src",URL.createObjectURL(file));
	});
	
	
	
	
	//
	
	
	/**************请求***************/
	
	
});





















